﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.UI;
using Silvermoon.Animations;

namespace Silvermoon.Controls.Classes
{
    internal class PropertyAnimation
    {
        public readonly Dictionary<PropertyAccessor, PropertyTracking> Properties = new Dictionary<PropertyAccessor, PropertyTracking>(4);
        protected readonly Queue<Animation> Queued = new Queue<Animation>(4);

        public GroupTransitions GroupTransitions;

        public void Dequeue(IPropertyObject obj)
        {
            while (Queued.Count > 0)
            {
                Animation a = Queued.Dequeue();
                a.IsEnqueued = false;
                a.Prepare(obj);
                a.InvokeAsync();
            }
        }

        public void Enqueue(Animation a)
        {
            if (!a.IsEnqueued)
            {
                a.IsEnqueued = true;
                Queued.Enqueue(a);
            }
        }

        internal void Clear(IPropertyObject obj)
        {
            var propertyValues = Properties;
            PropertyAccessor[] properties = new PropertyAccessor[propertyValues.Count];
            int n = 0;
            foreach (var pair in propertyValues)
            {
                properties[n++] = pair.Key;
            }

            foreach (PropertyAccessor property in properties)
            {
                property.EndAnimation(obj, -1);
            }
            propertyValues.Clear();
            if (GroupTransitions!=null)
            {
                GroupTransitions.Clear();
                GroupTransitions = null;
            };
        }
    }
}
